<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Serializer
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: PhpSerialize.php 20575 2010-01-24 17:48:27Z mabe $
 */

/** @see Zend_Serializer_Adapter_AdapterAbstract */
require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';

/**
 * @category   Zend
 * @package    Zend_Serializer
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Myzen_Serializer_Adapter_SimpleXML extends Zend_Serializer_Adapter_AdapterAbstract
{

    /**
     * Constructor
     * 
     * @param  array|Zend_Config $opts 
     * @return void
     */
    public function __construct($opts = array()) 
    {
        parent::__construct($opts);
    }

    /**
     * Serialize using serialize()
     * 
     * @param  only array no object 
     * @param  array $opts 
     * @return string
     * @throws Zend_Serializer_Exception On serialize error
     */
    public function serialize($value, array $opts = array())
    {
		if(!is_array($value)) {
			require_once 'Zend/Serializer/Exception.php';
            throw new Zend_Serializer_Exception('Only array can be serialized using simpleXML serializer', 29);
		}
		$XMLString = '<?xml version="1.0" encoding="utf-8" ?>'."\n";
		$XMLString .= '<rsp>'."\n";
		$XMLString .= self::_formatXML($value,0);
		$XMLString .= '</rsp>'."\n";
		
        return $XMLString;
    }
	
	protected static function _formatXML($value) {
		$XMLString = '';
		foreach($value as $key =>$data) {
			if(!is_numeric($key)) $XMLString .= '<'.$key.'>';
			else $XMLString .= '<elem>';
			
			if(is_array($data)) $XMLString .= "\n".self::_formatXML($data)."\n";
			else $XMLString .= self::_formatNonArray($data);
			
			if(!is_numeric($key)) $XMLString .= '</'.$key.'>'."\n";
			else $XMLString .= '</elem>';
		}
		return $XMLString;
	}
	
	protected static function _formatNonArray($value) {
		if(is_bool($value)) return (int) $value;
		elseif(is_int($value)) return $value;
		elseif(is_string($value)) return  htmlspecialchars($value,ENT_COMPAT,'UTF-8');
	}
	
    /**
     * Unserialize
     * 
     * @param  string $serialized 
     * @param  array $opts 
     * @return mixed array object if ok
     * @throws Zend_Serializer_Exception on unserialize error
     */
    public function unserialize($serialized, array $opts = array())
    {
        return array();
    }
}
